package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.Cargo;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class CargaDAO extends GenericDAO<Cargo>  {
	
	// fields
	public static String SELECT_BASICA = " SELECT Cargo.*" +
                                         " FROM Cargo ";
	
	// public methods
	public boolean delete(Cargo cargo) {
		String sql = SqlFactory.getSqlDelete("Cargo", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(cargo.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
	public Cargo get(int id) {
		Cargo cargo = null;
		String sql = SELECT_BASICA + " WHERE Cargo.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				cargo = new Cargo();
				loadAttribs(cargo, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return cargo;
	}
	public void loadAttribs(Cargo cargo, ResultSet r) {
		try {
			cargo.setId(r.getInt("id"));
			cargo.setDescricao(r.getString("descricao"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// protected methods
	protected boolean insert(Cargo cargo) {
		String sql = SqlFactory.getSqlInsert("Cargo");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(cargo.getId());
	 	params.add(cargo.getDescricao());
		try {
			cargo.setId(dataProvider.insert(sql, params));
			return cargo.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(Cargo cargo) {
		String sql = SqlFactory.getSqlUpdate("Cargo", "Cargo.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(cargo.getDescricao());
		params.add(cargo.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}

}